//package com.xnyy.lottery.security;
//
//import cn.hutool.json.JSONUtil;
//import com.xnyy.lottery.common.CommonResult;
//import com.xnyy.lottery.util.JwtUtils;
//import org.springframework.security.core.Authentication;
//import org.springframework.security.web.authentication.AuthenticationSuccessHandler;
//import org.springframework.stereotype.Component;
//
//import javax.servlet.ServletException;
//import javax.servlet.ServletOutputStream;
//import javax.servlet.http.HttpServletRequest;
//import javax.servlet.http.HttpServletResponse;
//import java.io.IOException;
//import java.nio.charset.StandardCharsets;
//
//@Component
//public class LoginSuccessHandler implements AuthenticationSuccessHandler {
//
//    private final JwtUtils jwtUtils;
//
//    public LoginSuccessHandler(JwtUtils jwtUtils) {
//        this.jwtUtils = jwtUtils;
//    }
//
//    @Override
//    public void onAuthenticationSuccess(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Authentication authentication) throws IOException, ServletException {
//        httpServletResponse.setContentType("application/json;charset=UTF-8");
//        ServletOutputStream outputStream = httpServletResponse.getOutputStream();
//
//        // 生成JWT，并放置到请求头中
//        String jwt = jwtUtils.generateToken(authentication.getName());
//        httpServletResponse.setHeader(jwtUtils.getHeader(), jwt);
//        //TODO 登录成功或服务器应该缓存token
//
//        CommonResult result = CommonResult.success("登录成功");
//
//        outputStream.write(JSONUtil.toJsonStr(result).getBytes(StandardCharsets.UTF_8));
//        outputStream.flush();
//        outputStream.close();
//    }
//}
